package action.member;

import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.IMember;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.Member;

public class MemberBrowAction extends MyBaseAction<Member> implements
		ModelDriven<Member> {

	private static final long serialVersionUID = 1L;
	private IMember iMember;
	private String mobileCode;
	private String verifyCode;
	private String oldpwd;
	private String password;
	private String password2;

	public void setiMember(IMember iMember) {
		this.iMember = iMember;
	}

	public void setMobileCode(String mobileCode) {
		this.mobileCode = mobileCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public MemberBrowAction() {
		super(Member.class);
	}

	public Member getModel() {
		return model;
	}

	/**
	 * 获取主页信息
	 */
	public void gethomedata() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iMember.getHomeData(userId);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 获取当前登录用户信息
	 */
	public void getmyinfo() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iMember.findById("Member", "*", "json", "userId", userId);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 发送手机验证码
	 */
	public void getmobilecode() {
		String r = "1";
		try {
			r = iMember.getMobileCodeForRegister(model.getMobile());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 发送手机验证码
	 */
	public void getmobilecodeforchangemobi() {
		String r = "1";
		try {
			r = iMember.getMobileCodeForChangeMobile(model.getMobile());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 注册
	 */
	public void register() {
		String r = "1";
		try {
			r = iMember.register(model, mobileCode, verifyCode, password,
					password2);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 发送手机验证码
	 */
	public void getmobilecodebyusername() {
		String r = "1";
		try {
			r = iMember.getMobileCodeByUserName(model.getUserName());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 找回密码
	 */
	public void forgetpwd() {
		String r = "1";
		try {
			r = iMember.forgetpwd(model, mobileCode, verifyCode, password,
					password2);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 修改密码
	 */
	public void changepwd() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iMember.changePwd(userId, oldpwd, password, password2,
					verifyCode);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 修改手机号
	 */
	public void changemobile() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iMember.changeMobile(userId, model.getMobile(), mobileCode,
					verifyCode);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

}
